<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <%--第一步--%>
    <!-- 获取CSRF Token -->
    <meta name="_csrf" content="${_csrf.token}"/>
    <!-- 获取CSRF头，默认为X-CSRF-TOKEN -->
    <meta name="_csrf_header" content="${_csrf.headerName}"/>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <%-- 搜索条件区域 --%>
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">部门名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="deptName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn"  lay-submit lay-filter="data-search-btn"><i class="layui-icon layui-icon-search"></i>搜索</button>
                            <button type="reset" class="layui-btn layui-btn-warm"><i class="layui-icon layui-icon-refresh-1"></i>重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <%-- 头部工具栏区域 --%>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加 </button>
            </div>
        </script>

        <%-- 表格区域 --%>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <%-- 行工具栏区域 --%>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
    </div>
</div>

<%-- 添加和修改窗口 --%>
<div style="display: none;padding: 5px" id="addOrUpdateWindow">
    <form class="layui-form" style="width:90%;" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-block">
                <input type="hidden" name="id">
                <input type="text" name="deptName" lay-verify="required" autocomplete="off"
                       placeholder="请输入部门名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门地址</label>
            <div class="layui-input-block">
                <input type="text" name="address" lay-verify="required" autocomplete="off" placeholder="请输入部门地址"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门备注</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="remark" id="content"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-row layui-col-xs12">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit"><span
                        class="layui-icon layui-icon-add-1"></span>提交
                </button>
                <button type="reset" class="layui-btn layui-btn-warm"><span
                        class="layui-icon layui-icon-refresh-1"></span>重置
                </button>
            </div>
        </div>
    </form>
</div>
<script src="${pageContext.request.contextPath}/statics/layui/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var tableIns = table.render({
            elem: '#currentTableId',
            url: '${pageContext.request.contextPath}/dept/list',
            toolbar: '#toolbarDemo',
            cols: [[
                {field: 'id', width: 120, title: "部门编号",align: 'center'},
                {field: 'deptName', minWidth: 120, title: '部门名称',align: 'center'},
                {field: 'address', minWidth: 150, title: '部门地址',align: 'center'},
                {field: 'createDate', minWidth: 120, title: '创建时间',align: 'center'},
                {field: 'remark', minWidth: 120, title: '备注',align: 'center'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            page: true,
            done: function (res, curr, count) {
                //判断当前页码是否是大于1并且当前页的数据量为0
                if (curr > 1 && res.data.length == 0) {
                    var pageValue = curr - 1;
                    //刷新数据表格的数据
                    tableIns.reload({
                        page: {curr: pageValue}
                    });
                }
            }
        });
        form.on("submit(data-search-btn)",function (data) {
            tableIns.reload({
                where:data.field,
                page:{
                    curr:1
                }
            });
            return false;
        });
        //添加
        //表头添加数据
        table.on('toolbar(currentTableFilter)',function (obj) {//lay-filter属性
            switch (obj.event) {
                case 'add':
                    openAddWindow();//添加
                    break;
            }
        });
        //定义添加和修改的url 以及弹出层变量
        let url,mainIndex;
        //添加方法
        function openAddWindow() {
            mainIndex = layer.open({
                type : 1, //弹出层类型
                title : '添加部门',
                area : ['500px','350px'],//宽高
                content : $('#addOrUpdateWindow'),//面板内容
                success : function () {
                    //清空表单数据
                    $('#dataFrm')[0].reset();
                    //设置表单提交的url
                    url = '/dept/add';
                }
            });
        }
        //监听表单提交事件
        form.on('submit(doSubmit)',function (data) {
            $.post(url,data.field,function (result) {
                console.log(result.message);
                if(result.success){
                    layer.msg(result.message,{icon:1});
                    layer.close(mainIndex);//关闭窗口
                    tableIns.reload();//刷新表格
                }else{
                    layer.msg(result.message,{icon:2});
                }
            },'json');
        });

        //2
        //获取<meta>标签中封装的CSRF Token
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        //将头中的CSRF Token信息进行发送
        $(document).ajaxSend(function (e,xhr,options) {
            xhr.setRequestHeader(header,token);
        });

        //修改
        //行编辑
        table.on('tool(currentTableFilter)',function (obj) {//与表格的lay-filter属性绑定
            let rowData = obj.data;
            if(obj.event == 'edit'){
                mainIndex = layer.open({
                    type : 1, //弹出层类型
                    title : '编辑部门',
                    area : ['550px','350px'],//宽高
                    content : $('#addOrUpdateWindow'),//面板内容
                    success : function () {
                        //设置表单提交的url
                        url = '/dept/update';
                        //给修改表单赋值
                        form.val('dataFrm',rowData);
                    }
                });
            }else if(obj.event == 'delete'){
                $.post("/dept/checkDeptByUser",{deptId:rowData.id},function (result) {
                    console.log(result);
                    if(result.exist){
                        layer.alert(result.message,{icon:0});
                    }else{
                        layer.confirm("确定删除该部门信息吗？",{icon:3,title:'请确认'},function (index) {
                            $.post("/dept/del",{deptId:rowData.id},function (result) {
                                if(result.success){
                                    layer.msg(result.message,{icon:1});
                                    tableIns.reload();
                                }else{
                                    layer.msg(result.message,{icon:2});
                                }
                            },'json');
                            layer.close(index);
                        });
                    }
                },'json');
            }
        })
        //删除
    });
</script>
</body>
</html>
